<?php
require_once ('../utility/appConsts.php');
class User {
	public $userName;
	function editUser($userid, $oldpassword, $newpassword) {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$arr = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		$oldPasswordOk = '';
		echo mysql_error ();
		$username = '';
		if ($db_found) {
			$SQL = "SELECT id,username,password,user_type FROM login_table where id=$userid";
			$result = mysql_query ( $SQL, $db_handle );
			$total = mysql_num_rows ( $result );
			if ($total > 0) {
				while ( $db_field = mysql_fetch_assoc ( $result ) ) {
					if ($oldpassword == $db_field ['password']) {
						$oldPasswordOk = $appConstants->USER_OLD_PASSWORD_OK;
						if ($appConstants->DEBUG) {
							echo 'Old password is ok' . "<br/>";
						}
						$username = $db_field ['username'];
						mysql_close ( $db_handle );
						break;
					} else {
						$oldPasswordOk = $appConstants->USER_OLD_PASSWORD_WRONG;
						$arr ['oldpassword'] = $oldPasswordOk;
						if ($appConstants->DEBUG) {
							echo 'Old password is not ok' . "<br/>";
						}
						mysql_close ( $db_handle );
						return $arr;
					}
				}
			}
			$arr ['username'] = $username;
			$arr ['newpassword'] = $newpassword;
			$arr ['oldpasswordok'] = $oldPasswordOk;
			return $this->updateUserPassword ( $arr );
		}
	}
	function updateUserPassword($userCredentialsArray) {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$arr = array ();
		$newpassword = $userCredentialsArray ['newpassword'];
		$username = $userCredentialsArray ['username'];
		$oldpasswordOk = $userCredentialsArray ['oldpasswordok'];
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		if ($db_found) {
			$SQL = "UPDATE login_table SET password = '$newpassword' where username = '$username' ";
			if ($appConstants->DEBUG) {
				echo $SQL;
			}
			if ($oldpasswordOk == $appConstants->USER_OLD_PASSWORD_OK) {
				$result = mysql_query ( $SQL, $db_handle );
				if ($result == "true") {
					$result = 1;
				}
				$arr ['passwordchanged'] = $result;
				return $arr;
			}
		}
	}
	function getUserDataFromUserTable() {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$arr = array ();
		$userArray = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		echo mysql_error ();
		$userId = '';
		$userType = '';
		$username = '';
		if ($db_found) {
			$SQL = "SELECT id,username,user_type FROM login_table";
			$result = mysql_query ( $SQL, $db_handle );
			$total = mysql_num_rows ( $result );
			$i = 0;
			if ($total > 0) {
				while ( $db_field = mysql_fetch_assoc ( $result ) ) {
					$arr ['userid'] = $db_field ['id'];
					$arr ['username'] = $db_field ['username'];
					$arr ['user_type'] = $db_field ['user_type'];
					$userArray [$i] = $arr;
					$i++;
				}
			}
			return $userArray;
		}
	}
	function updateUserTypeWithJSONString($json_str) {
		$stdInArray = json_decode ( $json_str, true );
		if ($appConstants->DEBUG) {
			print_r($stdInArray);
		}
		$stdInArray = $stdInArray['data'];
		$stdInArray = json_decode ($stdInArray,true);
		$count = count ( $stdInArray );
		$resultArr = array();
		for($i = 0; $i < $count ;$i++) {
			$user = $stdInArray[$i];
			$resultArr[$i] =$this -> updateUserTypeValue($user['userid'],$user['username'],$user['usertype']);
			
		}
		return $resultArr;
	}
	function updateUserTypeValue($userId, $userName, $userType) {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		if ($db_found) {
			$SQL = "UPDATE login_table SET user_type = $userType where id = $userId ";
			//if ($appConstants->DEBUG) {
				echo $SQL;
			//}
			$result = mysql_query ( $SQL, $db_handle );
			if ($result == "true") {
				$result = 1;
			}
			return $result;
		}
	}
	
	function getSearchedUserDataFromUserTable($emailAddress) {
		$appConstants = new appConstsClass ();
		$appConstants->initialize ();
		$arr = array ();
		$userArray = array ();
		$db_handle = mysql_connect ( $appConstants->SERVER, $appConstants->DBUSERNAME, $appConstants->DBPASSWORD );
		$db_found = mysql_select_db ( $appConstants->DBNAME, $db_handle );
		echo mysql_error ();
		$userId = '';
		$userType = '';
		$username = '';
		if ($db_found) {
			$SQL = "SELECT id,username,user_type,emailid FROM login_table where emailid='$emailAddress'";
			$result = mysql_query ( $SQL, $db_handle );
			$total = mysql_num_rows ( $result );
			$i = 0;
			if ($total > 0) {
				while ( $db_field = mysql_fetch_assoc ( $result ) ) {
					$arr ['userid'] = $db_field ['id'];
					$arr ['username'] = $db_field ['username'];
					$arr ['user_type'] = $db_field ['user_type'];
					$arr ['emailid'] = $db_field ['emailid'];
					$userArray [$i] = $arr;
					$i++;
				}
			}
			return $userArray;
		}
	}
}
?>